package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 修改护理计划
     */
    @Override
    public void updateNursingPlan(Long id,NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        //修改项目计划中间表
        //先删除
        nursingProjectPlanMapper.deleteProjectPlan(id);
        //再加入

        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        if (projectPlans!=null){
            for (NursingProjectPlanVo projectPlan : projectPlans) {
                projectPlan.setPlanId(id);
                projectPlan.setCreateTime(LocalDateTime.now());
            }
        }
        nursingProjectPlanMapper.updateProjectPlan(projectPlans);

    }


    /**
     * 删除护理计划
     */
    @Override
    public void deleteNursingPlan(Integer id) {
        nursingPlanMapper.deleteNursingPlan(id);
    }

    /**
     * 启用或禁用
     */
    @Override
    public boolean enableOrDisable(Integer id, Integer status) {
        // 查询计划是否存在
        NursingPlanVo nursingPlanVo = nursingPlanMapper.searchNursingPlanById(id);
        if (nursingPlanVo == null) {
            return false;
        }
        nursingPlanMapper.updateStatus(id, status);
        return true;

    }

    /**
     * 查询所有护理计划
     * @return
     */
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> list =  nursingPlanMapper.getAll();
        return list;
    }

    /**
     * 新增护理计划
     * @param nursingPlanDto
     * @return
     */
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        bean.setStatus(1);
        //先加入基本信息,记得要回退id
        nursingPlanMapper.add(bean);
        //然后再加入项目计划中间表信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        Long id = bean.getId();
        if (projectPlans!=null && projectPlans.size()>0){
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(id);
            }
            nursingProjectPlanMapper.addProjictPlan(projectPlans);
        }
    }

    /**
     * 护理计划分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> nursingPlanPaginationQuery(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> page=nursingPlanMapper.nursingPlanPaginationQuery(name,status);
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>();
        pageResponse.setTotal(page.getTotal());
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 根据id查询护理计划
     * @return
     */
    @Override
    public NursingPlanVo searchNursingPlanById(Integer id) {
        return nursingPlanMapper.searchNursingPlanById(id);
    }
}
